package com.leo.foodmaster.admin.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.foodmaster.admin.system.entity.SysDictData;
import com.leo.foodmaster.admin.system.mapper.SysDictDataMapper;
import com.leo.foodmaster.admin.system.service.ISysDictDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leo.foodmaster.admin.system.service.ISysDictTypeService;
import com.leo.foodmaster.commons.constants.AuthConstants;
import com.leo.foodmaster.commons.entity.Result;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 字典数据表 服务实现类
 * </p>
 *
 * @author Leo
 * @since 2021-06-29
 */
@Service
@AllArgsConstructor
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements ISysDictDataService {
    private final ISysDictTypeService dictTypeService;


    /**
     * 分页条件查询字典数据
     * @param page 分页参数
     * @param dictType 字典类型
     * @param name 字典数据名称
     * @param status 字典数据状态
     * @return IPage<SysDictData>
     */
    @Override
    public IPage<SysDictData> getDictDataPageList(Page<SysDictData> page, String dictType, String name, String status) {
        LambdaQueryWrapper<SysDictData> qw = Wrappers.lambdaQuery(SysDictData.class)
                .eq(StrUtil.isNotBlank(dictType), SysDictData::getDictType, dictType)
                .like(StrUtil.isNotBlank(name), SysDictData::getName, name)
                .eq(StrUtil.isNotBlank(status), SysDictData::getStatus, status);
        return page(page, qw);
    }

    /**
     * 新增字典数据
     * @param dictData 字典数据
     * @return Result<Object>
     */
    @Override
    public Result<Object> addSysDictData(SysDictData dictData) {
        // 新增字典数据
        boolean save = save(dictData);
        // 刷新缓存
        if (save) {
            refreshDictToCache(dictData);
        }
        return Result.success();
    }

    /**
     * 刷新 字典数据到缓存
     * @param dictData 字典数据
     */
    private void refreshDictToCache(SysDictData dictData) {
        // 根据字典类型查询字典数据列表
        List<SysDictData> dictDataList = list(Wrappers.lambdaQuery(SysDictData.class)
                .eq(SysDictData::getDictType, dictData.getDictType()));
        String dictKey = AuthConstants.SYS_DICT_KEY + dictData.getDictType();
        dictTypeService.refreshDictToCache(dictKey, dictDataList);
    }

    /**
     * 修改字典数据
     * @param dictData 字典数据
     * @return Result<Object>
     */
    @Override
    public Result<Object> updateSysDictData(SysDictData dictData) {
        // 修改字典数据
        boolean update = updateById(dictData);
        // 刷新缓存
        if (update) {
            refreshDictToCache(dictData);
        }
        return Result.success();
    }

    /**
     * 删除字典数据
     * @param dictDataIds 字典数据Ids
     * @return Result<Object>
     */
    @Override
    public Result<Object> deleteSysDictData(String dictDataIds) {
        String[] dictDataIdArray = dictDataIds.split(StrUtil.COMMA);
        List<Long> dictDataIdList = new ArrayList<>();
        for (String dictDataId : dictDataIdArray) {
            dictDataIdList.add(Long.parseLong(dictDataId));
        }
        // 删除字典数据
        boolean remove = removeByIds(dictDataIdList);
        // 刷新缓存
        if (remove) {
            dictTypeService.listAllDict();
        }
        return Result.success();
    }
}
